import { Body, Controller, Delete, Get, Post, Query } from '@nestjs/common';
import { PermissionService } from './permission.service';
import { SavePermissionDto } from './dto/permission.dto';
import { RequirePermission } from '../../../common/decorators/require-permission.decorator';
import {
  PageQueryParam,
  PaginationPipe,
} from '../../../common/pipes/pagination.pipe';
import { Like } from 'typeorm';

@Controller('permission')
export class PermissionController {
  constructor(private readonly permissionService: PermissionService) {}

  @RequirePermission('sys:permission:save')
  @Post('save')
  async save(@Body() body: SavePermissionDto) {
    return await this.permissionService.save(body);
  }

  @RequirePermission('sys:permission:find')
  @Get('find-page')
  async findPage(
    @Query(PaginationPipe) param: PageQueryParam,
    @Query('name') name?: string,
  ) {
    return await this.permissionService.findAndCount({
      where: { name: name ? Like(`%${name}%`) : undefined },
      ...param,
    });
  }

  @RequirePermission('sys:permission:delete')
  @Delete()
  async delete(@Body('ids') ids: string[]) {
    return await this.permissionService.delete(ids);
  }
}
